package client.ui;

import java.util.List;

import phasebook.client.msgs.UserProfileInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.tab.Tab;

public class UserTab extends Tab implements ResponseHandler{
	private boolean isHome;
	private Controller controller;
	private UserProfileInfo user;
	private ProfileSection profileSection;
	private BoardSection boardSection;
	private final ImagePicker picker;
	private EditorSection editorsection;

	public UserTab(Controller controller, UserProfileInfo user,boolean isHome, ImagePicker picker){
		super(user.getName());
		this.isHome = isHome;
		this.controller = controller;
		this.user = user;
		this.picker = picker;
		if(this.isHome)
			super.setIcon("[SKIN]/headerIcons/home.png");
		else
			super.setCanClose(true);
		
		profileSection = new ProfileSection(controller,user,isHome);
		boardSection = new BoardSection(controller, user);
		editorsection = new EditorSection(controller,user,picker);
		
		SectionStack stack = new SectionStack();
		stack.setVisibilityMode(VisibilityMode.MULTIPLE);
		stack.setWidth100();
		stack.setHeight100();
		stack.setAnimateSections(true);
		stack.addSection(profileSection);
		stack.addSection(boardSection);
		stack.addSection(editorsection);
		this.setPane(stack);
		
		controller.addResponseHandler(this);
	}

	public long close() {
		this.controller.forgetUser(user.getUserId());
		this.controller.removeResponseHandler(profileSection);
		this.controller.removeResponseHandler(boardSection);
		this.picker.removePickListener(editorsection);
		return user.getUserId();
	}

	@Override
	public void onResponse(CombinedResponse response) {
		List<UserProfileInfo> profiles = response.getProfiles();
		if (profiles!=null)
			for(UserProfileInfo profile:profiles)
				if (profile.getUserId()==user.getUserId())
					this.setTitle(profile.getName());
		
	}
}
